// import { fileURLToPath, URL } from "node:url";
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import compressPlugin from 'vite-plugin-compression'
import { visualizer } from 'rollup-plugin-visualizer'
// import { isProd } from "./src/config";

// const plugins = isProd ? [] : [visualizer()];

// https://vitejs.dev/config/
export default defineConfig({
  base: '/',
  plugins: [
    vue(),
    compressPlugin({
      verbose: true, // 默认即可
      disable: false, //开启压缩(不禁用)，默认即可
      deleteOriginFile: false, //删除源文件
      threshold: 10240, // 压缩前最小文件大小
      algorithm: 'gzip', // 压缩算法
      ext: '.gz', // 文件类型
    }),
    // ...plugins,
    visualizer({
      gzipSize: true,
      brotliSize: true,
      emitFile: false,
      filename: 'report.html',
      open: true,
    }),
    // globals,
  ],
  resolve: {
    alias: [
      {
        find: '@',
        replacement: path.resolve(__dirname, 'src'),
      },
    ],
    // alias: {
    //   // "@": fileURLToPath(new URL("./src", import.meta.url)),
    //   // "@": path.resolve(__dirname, "src"),
    // },
  },

  css: {
    preprocessorOptions: {
      less: {
        additionalData: `@import "${path.resolve(__dirname, 'src/styles/variables.less')}";`,
        javascriptEnabled: true,
      },
    },
  },

  server: {
    open: false,
    proxy: {
      '/api': {
        target: "http://127.0.0.1:3000",
        // target: 'https://api.zhangwurui.cn',
        changeOrigin: true,
        headers: {
          origin: 'https://zhangwurui.cn',
          referer: 'https://zhangwurui.cn',
        },
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  },

  build: {
    sourcemap: false,
    outDir: './admin.zhangwurui.cn',
    reportCompressedSize: true,
    rollupOptions: {
      // external: ["monaco-editor"],
      // external: ["ant-design-vue"],
      // plugins: [globals],
      output: {
        manualChunks(id) {
          const incs = (ds: string[]) => ds.some((d) => id.includes(`node_modules/${d}`))
          if (incs(['monaco-editor'])) {
            return 'monaco-editor'
          } else if (incs(['axios', 'lodash', 'marked', 'dayjs', 'highlight.js'])) {
            return 'basic'
          } else if (id.includes('node_modules')) {
            return 'vendor'
          }
        },
      },
    },
  },
})
